package com.novacloud.fresh.authentication.ticket;

import com.novacloud.fresh.authentication.exception.AccessException;
import com.novacloud.fresh.util.RegexUtil;
import org.jasig.cas.authentication.Authentication;
import org.jasig.cas.authentication.ContextualAuthenticationPolicy;
import org.jasig.cas.authentication.ContextualAuthenticationPolicyFactory;
import org.jasig.cas.services.ServiceContext;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @param
 * @author <a href="mailto:huzhanjun@novacloud.com">huzj</a>
 * @version : 1.0
 * @date 2015-12-15 17:11
 */
public class AuthenticationPolicyFactory implements ContextualAuthenticationPolicyFactory<ServiceContext> {

//    @Autowired
//    private UserAccessProjectService userAccessProjectService;

    @Override
    public ContextualAuthenticationPolicy<ServiceContext> createPolicy(final ServiceContext context) {
        return new ContextualAuthenticationPolicy<ServiceContext>() {

            @Override
            public ServiceContext getContext() {
                return context;
            }

            @Override
            public boolean isSatisfiedBy(final Authentication authentication) {
//                try {
//                    userAccessProjectService.getUserProject(authentication.getPrincipal().getId(), RegexUtil.getDomainName(context.getService().getId()));
//                } catch (AccessException e) {
//                    return false;
//                }
                return true;
            }
        };
    }
}
